home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byte1286.arc / VIEWSET.C < prev   
Text File  |  1986-09-03  |  10KB  |  243 lines

  1. /*****************************************************
  2. *        ViewSet.c  (DISPLAYS MANDELBROT-SET)
  3. *    by Peter B. Schroeder 12-10-85 (updated 1-29-86)
  4. *   11550 SW 108 Court  Miami, FL 33176 (305)238-5509
  5. * Program opens data file specified in command line or
  6. * by default VIEW1.DATA; reads first three values from
  7. * data file (x-y coordinates and range), then reads in
  8. * and decodes display datum for each pixel; displays
  9. * data as a 200x200 low-resolution array; shows x-y
  10. * coordinates and range; and, in all but last view,
  11. * asks for keyboeard input to continue; after input,
  12. * outlines area in next-smaller display, then waits
  13. * for CLOSEWINDOW event from close-window gadget.
  14. * By opening additional command-line interfaces,
  15. * several of these displays may be run in parallel.
  16. ******************************************************/
  17.  
  18. #include <stdio.h>
  19. #include <exec/types.h>
  20. #include <ctype.h>
  21. #include <intuition/intuition.h>
  22. #include <libraries/translator.h>
  23. #include <devices/narrator.h>
  24. long TranslatorBase;            /* For translation   */
  25. UBYTE allocationMap[]={1,2,4,8}; /* Audio channels   */
  26. extern struct MsgPort *CreatePort(); /* For speech   */
  27. struct narrator_rb spk;              /* For speech   */
  28. struct IntuitionBase *IntuitionBase;
  29. struct GfxBase *GfxBase;
  30. struct NewScreen NewScreen =
  31. {
  32.  0,                   /* LeftEdge                    */
  33.  0,                   /* TopEdge                     */
  34.  320,                 /* Width (low resolution)      */
  35.  200,                 /* Height                      */
  36.  4,                   /* Depth (16 colors)           */
  37.  0,1,                 /* DetailPen and BlockPen      */
  38.  NULL,                /* No special display modes    */
  39.  CUSTOMSCREEN,        /* Screen type (not WorkBench) */
  40.  NULL,                /* Use default font            */
  41.  "Mandelbrot Screen", /* Compiled as a text pointer  */
  42.  NULL,                /* No special screen gadgets   */
  43.  NULL,                /* No special CustomBitMap     */
  44. };
  45.  
  46. main(argc,argv)  int argc; char *argv[];
  47. {
  48.  struct Screen *Screen;
  49.  struct NewWindow NewWindow;
  50.  struct Window *Window;
  51.  struct RastPort *rp;
  52.  struct ViewPort *vp;
  53.  struct View *v;
  54.  struct IntuiMessage *message;
  55.  extern struct narrator_rb spk;
  56.  int x,y,l,count,class,rtncode,error;
  57.  extern long TranslatorBase;     /* Must be external */
  58.  char file[15],axis[4][20],*sp,trs[250];
  59.  unsigned char a,b;              /* Used for coding  */
  60.  FILE *InFile;
  61.  /* Open file from command line or default           */
  62.   if(argc<=1) strcpy(file,"VIEW1.DATA");
  63.   else strcpy(file,argv[1]);
  64.   InFile = fopen(file,"r");
  65.   if(InFile==NULL) exit(TRUE);
  66.   /* Read in coordinates and range                   */
  67.   for(x=0;x<3;x++) fgets(axis[x],20,InFile);
  68.   /* Translate speech string                         */
  69.   sp =
  70.  "Press any key to show area covered by next display.";
  71.   TranslatorBase=OpenLibrary("translator.library",0);
  72.   rtncode=Translate(sp,strlen(sp),trs,250);
  73.   /* Phonetic code now in string trs - CloseLibrary  */
  74.   CloseLibrary(TranslatorBase);
  75.   error=OpenDevice("narrator.device",0,&spk,0);
  76.   /* Set up narrator message                         */
  77.    spk.message.io_Message.mn_ReplyPort =
  78.                                  CreatePort("sayit",0);
  79.    spk.ch_masks=allocationMap; /* All audio channels */
  80.    spk.nm_masks=4;             /* Four of them       */
  81.    spk.message.io_Command=CMD_WRITE; /* What to do   */
  82.    spk.message.io_Data=trs;    /* What to verbalize  */
  83.    spk.message.io_Length=strlen(trs);
  84.   /* Open Intuition Library                          */
  85.   IntuitionBase = (struct IntuitionBase *)
  86.                     OpenLibrary("intuition.library",0);
  87.   if(IntuitionBase == NULL) exit(TRUE);
  88.   /* Open Graphics Library                           */
  89.   GfxBase =
  90.    (struct GfxBase *)OpenLibrary("graphics.library",0);
  91.    if(GfxBase == NULL) exit(TRUE);
  92.   /* Open Screen in Intuition                        */
  93.   if((Screen = (struct Screen *)OpenScreen(&NewScreen))
  94.              == NULL) exit(TRUE);
  95.   /* Initialize new window structure                 */
  96.   NewWindow. LeftEdge = 0;
  97.   NewWindow.TopEdge = 0;
  98.   NewWindow.Width = 290; /* don't cover screen width */
  99.   NewWindow.Height =200;
  100.   NewWindow.DetailPen = 0;
  101.   NewWindow.BlockPen = 1;
  102.   if(index(file,"W1")>=0)
  103.                 NewWindow.Title = "Mandelbrot View #1";
  104.   else NewWindow.Title = "Mandelbrot View";
  105.   NewWindow.Flags =WINDOWCLOSE|SMART_REFRESH|ACTIVATE|
  106.     GIMMEZEROZERO|WINDOWDRAG|WINDOWDEPTH|WINDOWSIZING|
  107.     NOCAREREFRESH;    /* See these flags in manual   */
  108.   /* Need RAWKEY for INKEY type function             */
  109.   /* Also need to know about CLOSEWINDOW events      */
  110.   NewWindow.IDCMPFlags = RAWKEY|CLOSEWINDOW;
  111.   NewWindow.Type = CUSTOMSCREEN;
  112.   NewWindow.FirstGadget = NULL; /* No special gadget */
  113.   NewWindow.CheckMark = NULL;   /* Don't need        */
  114.   NewWindow.Screen = Screen;
  115.   NewWindow.BitMap = NULL;      /* Use default       */
  116.   NewWindow.MinWidth = 100;
  117.   NewWindow.MinHeight = 25;
  118.   NewWindow.MaxWidth = 320;
  119.   NewWindow.MaxHeight = 200;
  120.   /* Open new window but end if can't have new one   */
  121.   if((Window = (struct Window *)OpenWindow(&NewWindow))
  122.              == NULL) exit(TRUE);
  123.   /* Need the RastPort pointer from window structure */
  124.   (struct RastPort *)rp = Window->RPort;
  125.   /* Also need ViewPort and View structure pointers  */
  126.   (struct ViewPort *)vp = ViewPortAddress(Window);
  127.   (struct View *)v = ViewAddress();
  128.   SetDrMd(rp,JAM1); /* Set draw mode to one pen      */
  129.   /* Get count value from file and color each pixel  */
  130.    for(y=1;y<200;y++)     /* Each row                */
  131.    {
  132.     for(x=0;x<200;x++)    /* Each column or pixel    */
  133.     {
  134.      a=getc(InFile);          /* Unsigned characters */
  135.      b=getc(InFile);          /* for coded numbers   */
  136.      count = a*255 + b;   /* Decode count            */
  137.      /* Select a color based on counts using color   *
  138.      ** function and color each pixel accordingly    */
  139.      SetAPen(rp,color(count)); /* Select a color     */
  140.      WritePixel(rp,x,y);       /* and color pixel    */
  141.     }
  142.    }
  143.   fclose(InFile);              /* Close data file    */
  144.   /* Show coordinates and range of present display   */
  145.   SetAPen(rp,1);               /* Text in white      */
  146.   Move(rp,210,30);
  147.   Text(rp," x =",4);
  148.   Move(rp,201,40);
  149.   Text(rp,axis[0],9);
  150.   Move(rp,210,55);
  151.   Text(rp," y =",4);
  152.   Move(rp,201,65);
  153.   Text(rp,axis[1],9);
  154.   Move(rp,210,80);
  155.   Text(rp,"range =",7);
  156.   Move(rp,201,90);
  157.   Text(rp,axis[2],9);
  158.   /* Determine coordinates of next-smaller display    */
  159.   l=0; if(index(file,"W1")>=0)
  160.   { x=80; y=108;l=1; }            /* First view       */
  161.   else if(index(file,"W2")>=0)
  162.   { x=150;y=64; l=1; }            /* Second view      */
  163.   else if(index(file,"W3")>=0)
  164.   { x=170;y=1;  l=1; }            /* Third view       */
  165.   else if(index(file,"W4")>=0)
  166.   { x=140;y=100;l=1; }            /* Fourth view      */
  167.   if(l>0)
  168.   {
  169.    DoIO(&spk); /* speak and wait for any key pressed  */
  170.    Wait(1<<Window->UserPort->mp_SigBit);
  171.    /* Draw box showing next smaller display, if any   */
  172.    Move(rp,x,y);
  173.    x += 20;
  174.    Draw(rp,x,y);
  175.    y += 20;
  176.    Draw(rp,x,y);
  177.    x -= 20;
  178.    Draw(rp,x,y);
  179.    y -= 20;
  180.    Draw(rp,x,y);
  181.   }
  182.   /* Wait here for CLOSEWINDOW event, ignore RAWKEY  */
  183.   cycle:
  184.    Wait(1<<Window->UserPort->mp_SigBit); /*Wait input*/
  185.    message = GetMsg(Window->UserPort);   /* Get it   */
  186.    class = message->Class;             /* What is it */
  187.    if(class==RAWKEY)  goto cycle;   /* Ignore RAWKEY */
  188.     /* If CLOSEWINDOW event then end program         */
  189.     DeletePort(spk.message.io_Message.mn_ReplyPort);
  190.     CloseDevice(&spk);
  191.     CloseWindow(Window);
  192.     CloseScreen(Screen);
  193.     CloseLibrary(GfxBase);
  194.     CloseLibrary(IntuitionBase);
  195.     exit(TRUE);
  196. } /*end main*/
  197.  
  198. /* Function index from Kernighan and Ritchie p.68    */
  199.  index(s,t) char s[],t[];
  200.  {
  201.   int i,j,k;
  202.   for(i=0;s[i]!='\0';i++)
  203.   {
  204.    for(j=i,k=0;t[k]!='\0' && s[j]==t[k];j++,k++);
  205.    if(t[k]=='\0') return(i);
  206.   } 
  207.   return(-1);
  208.  } /* End function index                             */
  209.  
  210. /* Function color (assigns color to counts).         *
  211. ** Input: integer count. Output integer nc (color)   *
  212. ** This code can be modified to give the desired     *
  213. ** colors for different counts and ranges of counts. */
  214.  int color(count) int count;
  215.  {
  216.   int nc;                        /*  2 = black       */
  217.    if(count>995) nc=9;           /*  3 = orange      */
  218.    else if(count>400) nc =  1;   /*  4 = bright blue */
  219.    else if(count>350) nc = 10;   /*  5 = violet      */
  220.    else if(count>300) nc =  4;   /*  6 = sky blue    */
  221.    else if(count>250) nc =  3;   /*  7 = white       */
  222.    else if(count>200) nc =  8;   /*  8 = brown       */
  223.    else if(count>150) nc = 11;   /*  9 = red         */
  224.    else if(count>100) nc = 13;   /* 10 = green       */
  225.    else if(count> 90) nc =  6;   /* 11 = yellow      */
  226.    else if(count> 80) nc = 15;   /* 12 = bright blue */
  227.    else if(count> 70) nc =  5;   /* 13 = grey blue   */
  228.    else if(count> 60) nc =  2;   /* 14 = green       */
  229.    else if(count> 50) nc =  7;   /* 15 = grey        */
  230.    else if(count> 45) nc = 12;
  231.    else if(count> 40) nc =  0;
  232.    else if(count> 35) nc = 15;
  233.    else if(count> 30) nc =  3;
  234.    else if(count> 20) nc = 11;
  235.    else if(count> 10) nc =  4;
  236.    else nc = 14;
  237.    if(nc ==  4 && count % 2 == 0) nc = 8;
  238.    if(nc == 14 && count % 2 == 0) nc = 8;
  239.   return(nc);
  240.  } /*End function color */
  241.  
  242.